/*-----------------------------------------------------------------------------------
Name: Sangyoon Park
Date: January 25 2023
This do file does : 
	Setup dataset for displacement analysis
-----------------------------------------------------------------------------------*/
clear all
set matsize 10000
set maxvar 10000
set more off


#delim ;

*** Load and merge datasets;

use "${data_path}/FARMER_COMBINESURVEY_CLEAN";

preserve;
sort strata st_tgroup;
egen group_id = group(strata st_tgroup);
keep farmerid latitude longitude group_id;
tempfile farmer_gis;
save `farmer_gis';
restore;

merge 1:1 farmerid using "${data_path}/FARMER_COMBINETRADE_AVERAGE";
drop _merge;

merge 1:1 farmerid using "${data_path}/PEST_HV_MRL";
gen pest_sample = (_merge == 3);
drop _merge;

merge m:1 strata st_tgroup using "${data_path}/INTERMEDIARY_GROUPAVG";
drop _merge;


*** Generate variables;

gen comply_eu = (sum_overmrl_eu == 0);
gen comply_us = (sum_overmrl_us == 0);
gen comply_china = (sum_overmrl_china == 0);
gen comply_japan = (sum_overmrl_japan == 0);

replace tradeprice_base = price_base if tradeprice_base == . & volume_base !=.;

drop price_base avgprice_fu1 avgprice_fu2 ;

rename tradeprice_base price_base;
rename tradeprice_fu1 price_fu1;
rename tradeprice_fu2 price_fu2;
rename traderev_base imp_revenue_base;
rename traderev_fu1 imp_revenue_fu1;
replace imp_revenue_fu1 = . if imp_revenue_fu1 == . & attrit_fu1 == 0;
rename traderev_fu2 imp_revenue_fu2;
replace imp_revenue_fu2 = . if imp_revenue_fu2 == . & attrit_fu2 == 0;
rename totrev_base revenue_base;
rename totrev_fu1 revenue_fu1;
rename totrev_fu2 revenue_fu2;

*** Generate new variables;

** received secondary education;
gen edu_second = (education >= 3);

** prior certificate;
gen anycert_base = (vietgap == 1 | globalgap == 1 | eurogap == 1 | othercert == 1);

local market "china asia eu dom";

	foreach y of local market {;
	
		gen export_`y'_base = (market_`y'_base > 0 & market_`y'_base != .);
		gen export_`y'_fu1 = (market_`y'_fu1 > 0 & market_`y'_fu1 != .);
		gen export_`y'_fu2 = (market_`y'_fu2 > 0 & market_`y'_fu2 != .);
		replace export_`y'_fu1 = . if attrit_fu1 == 1;
		replace export_`y'_fu2 = . if attrit_fu2 == 1;
		replace export_`y'_fu1 = . if market_`y'_fu1 == .;
		replace export_`y'_fu2 = . if market_`y'_fu2 == .;
		gen volume_`y'_base = volume_base*market_`y'_base;
		gen volume_`y'_fu1 = volume_fu1*market_`y'_fu1;
		gen volume_`y'_fu2 = volume_fu2*market_`y'_fu2;
		gen log_volume_`y'_base = log(volume_`y'_base+1);
		gen log_volume_`y'_fu1 = log(volume_`y'_fu1+1);
		gen log_volume_`y'_fu2 = log(volume_`y'_fu2+1);
		
		};

rename volume_base totalvolume_base;
rename volume_fu1 totalvolume_fu1;
rename volume_fu2 totalvolume_fu2;		
	

forvalues i = 1(1)2 {;
    replace hours_fu`i' = round(hours_fu`i', 1);
	};

** combine investment categories;

	gen cost_invest_base = invest_land_base + invest_plant_base + invest_post_base;
	
forvalues i = 1(1)2 {;
	gen cost_invest_fu`i' = invest_land_fu`i' + invest_plant_fu`i' + invest_post_fu`i';
	};
	
** combine cost categories;	
	gen cost_facequip_base = cost_facility_base + cost_equip_base;
	gen cost_labutil_base = cost_labor_base + cost_utility_base;
	gen cost_costtotal_base = cost_fert_base + cost_pest_base + cost_facequip_base + cost_labutil_base + cost_other_base;

forvalues i = 1(1)2 {;
	gen cost_facequip_fu`i' = cost_facility_fu`i' + cost_equip_fu`i';
	gen cost_labutil_fu`i' = cost_labor_fu`i' + cost_utility_fu`i';
	gen cost_costtotal_fu`i' = cost_fert_fu`i' + cost_pest_fu`i' + cost_facequip_fu`i' + cost_labutil_fu`i' + cost_other_fu`i';
	};

	
** implied total profit;
forvalues i = 1(1)2 {;
	gen profit_fu`i' = revenue_fu`i' - cost_costtotal_fu`i' if totalvolume_fu`i' != 0;
	sum profit_fu`i', detail;
	local min_profit_fu`i' = r(p1);
	gen pos_profit_fu`i' = profit_fu`i' - `min_profit_fu`i'';
	gen orig_profit_fu`i' = profit_fu`i';
	*replace profit_fu`i' = pos_profit_fu`i';
	gen imp_profit_fu`i' = imp_revenue_fu`i' - cost_costtotal_fu`i' if totalvolume_fu`i' != 0;
	sum imp_profit_fu`i', detail;
	local min_profit_fu`i' = r(p1);
	gen pos_imp_profit_fu`i' = imp_profit_fu`i' - `min_profit_fu`i'';
	gen orig_imp_profit_fu`i' = imp_profit_fu`i';
	*replace imp_profit_fu`i' = pos_imp_profit_fu`i';
	};
	
** generate log variables;
forvalues i = 1(1)2 {;
	gen log_totalvolume_fu`i' = log(totalvolume_fu`i'+1);
	gen log_price_fu`i' = log(price_fu`i'+1);
	gen log_revenue_fu`i' = log(revenue_fu`i'+1);
	gen log_imp_revenue_fu`i' = log(imp_revenue_fu`i'+1);
	gen log_invest_fu`i' = log(cost_invest_fu`i'+ 1);
	gen log_fert_fu`i' = log(cost_fert_fu`i'+1);
	gen log_pest_fu`i' = log(cost_pest_fu`i'+1);
	gen log_facility_fu`i' = log(cost_facility_fu`i' + 1);
	gen log_equip_fu`i' = log(cost_equip_fu`i' + 1);
	gen log_facequip_fu`i' = log(cost_facequip_fu`i'+1);
	gen log_labor_fu`i' = log(cost_labor_fu`i' + 1);
	gen log_utility_fu`i' = log(cost_utility_fu`i' + 1);
	gen log_labutil_fu`i' = log(cost_labutil_fu`i'+1);
	gen log_costtotal_fu`i' = log(cost_costtotal_fu`i'+1);
	gen log_profit_fu`i' = log(pos_profit_fu`i'+1);
	gen log_imp_profit_fu`i' = log(pos_imp_profit_fu`i'+1);
	};
	
local costvar "fert pest facequip labor utility";

foreach y of local costvar{;

	gen log_cost_`y'_base = log(cost_`y'_base + 1);
	
	};
	
** unit scale;
local costvar "fert pest facility equip labor utility costtotal";

foreach y of local costvar{;
forvalues i = 1(1)2 {;

	replace cost_`y'_fu`i' = cost_`y'_fu`i'/1000000;
	
	};
	};
	
local revvar "revenue imp_revenue profit imp_profit orig_profit orig_imp_profit";

foreach y of local revvar{;
forvalues i = 1(1)2 {;

	replace `y'_fu`i' = `y'_fu`i'/1000000;
	
	};
	};
	
forvalues i = 1(1)2 {;

	replace price_fu`i' = price_fu`i'/1000;
	
	};	

local volumevar "volume_dom volume_china volume_asia volume_eu totalvolume";

foreach y of local volumevar{;	
forvalues i = 1(1)2 {;

	replace `y'_fu`i' = `y'_fu`i'/1000;
	
	};	
	};

	
	
egen gap_comply_base = rmean(record_yield_base record_chemical_base pesticide_base irrigation_base storage_base cementpost_base treatment_base lighting_base coverfruit_base);
drop record_yield_base-coverfruit_base;

egen mean_trust_base = rmean(trust_1-trust_6);
drop trust_1-trust_6;

gen business_3_reverse = 6 - business_3;
gen business_4_reverse = 6 - business_4;
gen business_6_reverse = 6 - business_6;
gen business_7_reverse = 6 - business_7;
gen business_10_reverse = 6 - business_10;

egen mean_business_base = rmean(business_1 business_2 business_3_reverse business_4_reverse business_5 business_6_reverse business_7_reverse business_8 business_9 business_10_reverse business_11);
drop business_1-business_11;
drop business_3_reverse-business_10_reverse;

egen mean_confidence_base = rmean(confidence_1-confidence_10);
drop confidence_1-confidence_10;

gen present_bias_base = (present_discount > future_discount);
gen time_consistent_base = (present_discount == future_discount);

gen loan_any_base = (loan_bank_base == 1 | loan_farmer_base == 1| loan_trader_base == 1| loan_supplier_base == 1| loan_friend_base == 1| loan_other_base == 1);


gen perception_e6_rev = 6 - perception_e6;
gen perception_e8_rev = 6 - perception_e8;
egen mean_perception_fu2 = rmean(perception_e1 perception_e2 perception_e4 perception_e5 perception_e6_rev perception_e7 perception_e8_rev perception_e9);
drop perception_e1-perception_e10 perception_e6_rev perception_e8_rev;

	gen std_perception = .;
	
	forvalues j = 1(1)13 {;
	
	summ mean_perception_fu2 if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_perception = (mean_perception_fu2 - `var_mean')/`var_sd' if strata == `j';
	};

	gen raw_perception = mean_perception_fu2;
	drop mean_perception_fu2;
	
	gen std_knowledge = .;
	
	forvalues j = 1(1)13 {;
	
	summ gaptest_score_fu1 if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_knowledge = (gaptest_score_fu1 - `var_mean')/`var_sd' if strata == `j';
	};
	
	gen raw_knowledge = gaptest_score_fu1;
	drop gaptest_score_fu1;

	forvalues i = 1(1)5 {;
	replace barrier_q`i' = 0 if barrier_q`i' == .;
	};
	gen barrier_sum = barrier_q1 + barrier_q2 + barrier_q3 + barrier_q4 + barrier_q5;
	
	gen std_barrier = .;
	
	forvalues j = 1(1)13 {;
	
	summ barrier_sum if st_tgroup == 1;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_barrier = (barrier_sum - `var_mean')/`var_sd' if strata == `j';
	};
	
** mean of scores and measures on product quality;


forvalues i = 1(1)2 {;
	gen audit_score_fu`i' = audit_total_fu`i'/32;
	gen std_score_fu`i' = .;
	
	forvalues j = 1(1)13 {;
	
	summ audit_score_fu`i' if st_tgroup == 1 & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_score_fu`i' = (audit_score_fu`i' - `var_mean')/`var_sd' if strata == `j';
	};
	
	egen audit_hygman_fu`i' = rmean(audit_c5_a_fu`i' audit_c5_b_fu`i' audit_c5_c_fu`i' audit_c5_d_fu`i' audit_c5_e_fu`i');
	gen std_hygman_fu`i' = .;
	
	forvalues j = 1(1)13 {;
	
	summ audit_hygman_fu`i' if st_tgroup == 1 & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_hygman_fu`i' = (audit_hygman_fu`i' - `var_mean')/`var_sd' if strata == `j';
	};
	
	egen audit_landman_fu`i' = rmean(audit_c6_a_fu`i' audit_c6_b_fu`i' audit_c6_c_fu`i' audit_c6_d_fu`i' audit_c6_e_fu`i' audit_c10_a_fu`i' audit_c10_b_fu`i' audit_c10_c_fu`i');
	gen std_landman_fu`i' = .;
	
	forvalues j = 1(1)13 {;
	
	summ audit_landman_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_landman_fu`i' = (audit_landman_fu`i' - `var_mean')/`var_sd' if strata == `j';
	};
	
	egen audit_pestman_fu`i' = rmean(audit_c9_a_fu`i' audit_c9_b_fu`i' audit_c9_c_fu`i' audit_c9_d_fu`i' audit_c9_e_fu`i' audit_c9_f_fu`i' audit_c9_g_fu`i' audit_c9_h_fu`i' audit_c7_fu`i');
	gen std_pestman_fu`i' = .;
	
	forvalues j = 1(1)13 {;
	
	summ audit_pestman_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_pestman_fu`i' = (audit_pestman_fu`i' - `var_mean')/`var_sd' if strata == `j';
	};
	
	egen audit_equipman_fu`i' = rmean(audit_c1_a_fu`i' audit_c1_b_fu`i' audit_c1_c_fu`i' audit_c2_fu`i' audit_c3_fu`i' audit_c4_a_fu`i' audit_c4_b_fu`i');
	gen std_equipman_fu`i' = .;
	
	forvalues j = 1(1)13 {;
	
	summ audit_equipman_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_equipman_fu`i' = (audit_equipman_fu`i' - `var_mean')/`var_sd' if strata == `j';
	};
	
	egen audit_fertman_fu`i' = rmean(audit_c8_a_fu`i' audit_c8_b_fu`i' audit_c8_c_fu`i');
	gen std_fertman_fu`i' = .;
	
	forvalues j = 1(1)13 {;
	
	summ audit_fertman_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_fertman_fu`i' = (audit_fertman_fu`i' - `var_mean')/`var_sd' if strata == `j';
	};

	gen sum_pestman_fu`i' = audit_c9_a_fu`i' + audit_c9_b_fu`i' + audit_c9_c_fu`i' + audit_c9_d_fu`i' + audit_c9_e_fu`i' + audit_c9_f_fu`i' + audit_c9_g_fu`i' + audit_c9_h_fu`i' + audit_c7_fu`i';	
	gen sum_hygman_fu`i' = audit_c5_a_fu`i' + audit_c5_b_fu`i' + audit_c5_c_fu`i' + audit_c5_d_fu`i' + audit_c5_e_fu`i';
	gen sum_landman_fu`i' = audit_c6_a_fu`i' + audit_c6_b_fu`i' + audit_c6_c_fu`i' + audit_c6_d_fu`i' + audit_c6_e_fu`i' + audit_c10_a_fu`i' + audit_c10_b_fu`i' + audit_c10_c_fu`i';
	gen sum_fertman_fu`i' = audit_c8_a_fu`i' + audit_c8_b_fu`i' + audit_c8_c_fu`i';
	gen sum_equipman_fu`i' = audit_c1_a_fu`i' + audit_c1_b_fu`i' + audit_c1_c_fu`i' + audit_c2_fu`i' + audit_c3_fu`i' + audit_c4_a_fu`i' + audit_c4_b_fu`i';
	
	egen avgsweet_fu`i' = rmean(sweet_sam1_top_fu`i' sweet_sam1_mid_fu`i' sweet_sam1_bot_fu`i' sweet_sam2_top_fu`i' sweet_sam2_mid_fu`i' sweet_sam2_bot_fu`i');

	egen avgskin_fu`i' = rmean(color_sk_sam1_fu`i' color_sk_sam2_fu`i');

	egen avgbract_fu`i' = rmean(color_br_sam1_fu`i' color_br_sam2_fu`i');

	egen avgweight_fu`i' = rmean(weight_sam1_fu`i' weight_sam2_fu`i');

	egen avglength_fu`i' = rmean(length_sam1_fu`i' length_sam2_fu`i');

	egen avgwidth_fu`i' = rmean(width_sam1_fu`i' width_sam2_fu`i');
	
	gen std_sweet_fu`i' = .;
	gen std_skin_fu`i' = .;
	gen std_bract_fu`i' = .;
	gen std_length_fu`i' = .;
	gen std_width_fu`i' = .;
	gen std_weight_fu`i' = .;
	
	forvalues j = 1(1)13 {;
	
	summ avgsweet_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_sweet_fu`i' = (avgsweet_fu`i' - `var_mean')/`var_sd' if strata == `j';
	
	summ avgskin_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_skin_fu`i' = (avgskin_fu`i' - `var_mean')/`var_sd' if strata == `j';
	
	summ avgbract_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_bract_fu`i' = (avgbract_fu`i' - `var_mean')/`var_sd' if strata == `j';
	
	summ avglength_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_length_fu`i' = (avglength_fu`i' - `var_mean')/`var_sd' if strata == `j';
	
	summ avgwidth_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_width_fu`i' = (avgwidth_fu`i' - `var_mean')/`var_sd' if strata == `j';
	
	summ avgweight_fu`i' if st_tgroup == 1  & strata <= 11;
	local var_mean = r(mean);
	local var_sd = r(sd);
	replace std_weight_fu`i' = (avgweight_fu`i' - `var_mean')/`var_sd' if strata == `j';

	};
	
	egen std_meanquality_fu`i' = rmean(std_sweet_fu`i' std_skin_fu`i' std_bract_fu`i' std_length_fu`i' std_width_fu`i' std_weight_fu`i');
	gen avgmeanquality_fu`i' = std_meanquality_fu`i';	
	};
	

save "${data_path}/FARMER_WIDE_SETUP", replace;	


reshape long std_score std_hygman std_landman std_pestman std_equipman std_fertman audit_score audit_hygman audit_landman audit_pestman audit_equipman audit_fertman std_sweet std_skin std_bract std_length std_width std_weight std_meanquality avgsweet avgskin avgbract avgweight avglength avgwidth avgmeanquality export_china volume_china log_volume_china export_asia volume_asia log_volume_asia export_eu volume_eu log_volume_eu export_dom volume_dom log_volume_dom price totalvolume profit imp_profit orig_profit orig_imp_profit revenue cost_costtotal log_price imp_revenue log_imp_revenue log_totalvolume log_revenue log_costtotal log_profit log_imp_profit cost_fert cost_pest cost_facility cost_equip cost_labor cost_utility hours log_fert log_pest log_facility log_equip log_labor log_utility, i(farmerid) j(followup) string;

gen round = 0 if followup == "_base";
replace round = 1 if followup == "_fu1";
replace round = 2 if followup == "_fu2";

local varbase "price totalvolume revenue hours cost_fert cost_pest cost_facility cost_equip cost_labor cost_utility";

foreach y of local varbase {;
	
	bys farmerid: gen `y'_base = `y' if followup == "_base";
	bys farmerid: replace `y'_base = `y'_base[_n-1] if followup == "_fu1";
	bys farmerid: replace `y'_base = `y'_base[_n-1] if followup == "_fu2";

	};

	
local gapvar "std_score std_equipman std_hygman std_landman std_pestman std_fertman audit_score audit_equipman audit_hygman audit_landman audit_pestman audit_fertman";

foreach y of local gapvar {;
	
	bys farmerid: gen `y'_fu1 = `y' if followup == "_fu1";
	bys farmerid: gen `y'_fu2 = `y' if followup == "_fu2";

	};		
	
local exportvar "export_dom export_china export_asia export_eu volume_dom volume_china volume_asia volume_eu totalvolume log_volume_dom log_volume_china log_volume_asia log_volume_eu log_totalvolume";

foreach y of local exportvar {;
	
	bys farmerid: gen `y'_fu1 = `y' if followup == "_fu1";
	bys farmerid: gen `y'_fu2 = `y' if followup == "_fu2";

	};	
	
local costvar "hours cost_fert cost_pest cost_facility cost_equip cost_labor cost_utility cost_costtotal log_fert log_pest log_facility log_equip log_labor log_utility log_costtotal";

	
foreach y of local costvar {;
	
	bys farmerid: gen `y'_fu1 = `y' if followup == "_fu1";
	bys farmerid: gen `y'_fu2 = `y' if followup == "_fu2";

	};	
	
	
keep if round != .;

tempfile farmer_long;
save `farmer_long';
clear;

*** Calculate distance between farmers in treatment and control group;
use "${data_path}/FARMER_COMBINESURVEY_CLEAN";
sort strata st_tgroup;
egen group_id = group(strata st_tgroup);
preserve;
keep if st_tgroup == 4 | st_tgroup == 8;
keep farmerid latitude longitude group_id;
rename longitude longitude0;
rename latitude latitude0;
rename farmerid farmerid0;
tempfile treated_farmer;
save `treated_farmer';
clear;
restore;

preserve;
drop if st_tgroup == 4 | st_tgroup == 8;

geonear farmerid latitude longitude using `treated_farmer', n(farmerid0 latitude0 longitude0) within(2) long;
sort farmerid km_to_farmerid;
bys farmerid: keep if _n == 1;
drop farmerid0;
rename km_to_farmerid0 dist_nearest_treated;
tempfile dist_nearest_treated;
save `dist_nearest_treated';
restore;

rename longitude longitude0;
rename latitude latitude0;
rename farmerid farmerid0;

geonear farmerid0 latitude0 longitude0 using `farmer_gis', n(farmerid latitude longitude) within(2) long ign;
gen farmer_in2 = 1;
collapse (sum) farmer_in2, by(farmerid0);
rename farmerid0 farmerid;
tempfile farmer_in3;
save `farmer_in3';

clear;


*** Calculate distance between farmers and exporters ;

use "${data_path}/INTERMEDIARY_COMBINESURVEY_CLEAN";
sort strata st_tgroup;
egen group_id0 = group(strata st_tgroup);
keep traderid latitude longitude group_id0;
rename latitude latitude0;
rename longitude longitude0;
tempfile trader_gis;
save `trader_gis';

use `farmer_gis', clear;
	forvalues i = 1/5 {;
		preserve;
		geonear farmerid latitude longitude using `trader_gis', n(traderid latitude0 longitude0) within(`i') long;
		merge m:1 traderid using `trader_gis';
		drop _merge;
		merge m:1 farmerid using `farmer_gis';
		drop if group_id == group_id0;
		drop _merge;
		gen trader_in`i' = 1;
		collapse (sum) trader_in`i', by(farmerid);
		tempfile trader_in`i';
		save `trader_in`i'';
		restore;
	};

clear;

*** Calculate distance between farmers and exporters selling to Asia or EU;

use "${data_path}/INTERMEDIARY_LONG";
sort strata st_tgroup;
egen group_id0 = group(strata st_tgroup);
keep if volume_asia_fu0 > 0 | volume_eu_fu0 > 0;
bys traderid: keep if _n == 1;
keep traderid latitude longitude group_id0;
rename latitude latitude0;
rename longitude longitude0;
tempfile trader_gis;
save `trader_gis';

use `farmer_gis', clear;
	forvalues i = 1/5 {;
		preserve;
		geonear farmerid latitude longitude using `trader_gis', n(traderid latitude0 longitude0) within(`i') long;
		merge m:1 traderid using `trader_gis';
		drop _merge;
		merge m:1 farmerid using `farmer_gis';
		drop if group_id == group_id0;
		drop _merge;		
		gen hp_trader_in`i' = 1;
		collapse (sum) hp_trader_in`i', by(farmerid);
		tempfile hp_trader_in`i';
		save `hp_trader_in`i'';
		restore;
	};

clear;


*** Calculate distance between farmers and nearest exporter in joint training ;

use "${data_path}/INTERMEDIARY_COMBINESURVEY_CLEAN";
keep if inlist(st_tgroup,4,8);
keep traderid latitude longitude;
rename latitude latitude0;
rename longitude longitude0;
tempfile trader_gis;
save `trader_gis';

use `farmer_gis', clear;
		geonear farmerid latitude longitude using `trader_gis', n(traderid latitude0 longitude0) nea(3) long;
		collapse (mean) dist_jointtrader = km_to_traderid, by(farmerid);
		tempfile dist_jointtrader;
		save `dist_jointtrader';
	
clear;


use `farmer_long';

	forvalues i = 1/5 {;
		merge m:1 farmerid using `trader_in`i'';
		drop _merge;
		replace trader_in`i' = 0 if trader_in`i' == .;
		merge m:1 farmerid using `hp_trader_in`i'';
		drop _merge;
		replace hp_trader_in`i' = 0 if hp_trader_in`i' == .;
	};

	merge m:1 farmerid using `dist_jointtrader';
	drop _merge;
	
	merge m:1 farmerid using `dist_nearest_treated';
	drop _merge;
	merge m:1 farmerid using `farmer_in3';
	drop _merge;


save "${data_path}/FARMER_LONG_SETUP_V2", replace;






